<template>
    <div>
        <el-container class="main">
        <el-aside width="fit-content">
          <el-menu
            :unique-opened="true"
            router
            @select="changeTag"
            :default-active="$router.currentRoute.path"
            class="el-menu-vertical-demo"
            :collapse="isCollapse"
            background-color="#304156"
            text-color="#BFCBD9"
            :transition="{'transition-timing-function': 'ease-in!important'}"
            active-text-color="#1B8DFF">
            <el-menu-item class="navTopLogo">
              <div class="leftNavLogo">
                <el-image src="https://demo2.yixiang.co/static/img/logo.09e3e0bf.png" style="height:32px;width:32px"></el-image>
                <div v-if="!isCollapse">YSHOP-后台管理</div>
              </div>
            </el-menu-item>
            <el-menu-item class="indexMargin" index="/index">
              <i class="iconfont icon-huaban"></i>
              <span slot="title">首页</span>
            </el-menu-item>
            <el-submenu
            :index="item.path"
            v-for="item in leftNavList"
            :key="item.path">
              <template slot="title">
                <i :class="'iconfont '+item.meta.icon"></i>
                <span>{{item.meta.title}}</span>
              </template>
              <el-menu-item-group class="deepbg" v-for="items in item.children" :key="items.path">
                <el-menu-item class="navItem" :index="items.path">
                  <i :class="'iconfont '+items.meta.icon"></i>
                  <span slot="title">{{items.meta.title}}</span>
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
            <el-container style="height:100vh">
                <el-header>
                  <div class="header_top">
                      <div class="CollapseMenu_Breadcrumb">
                          <i v-if="isCollapse" @click="CollapseMenu" class="iconfont icon-zhankai4"></i>
                          <i v-if="!isCollapse" @click="CollapseMenu" class="iconfont icon-shouqi"></i>
                          <div class="Breadcrumb">
                              <el-breadcrumb>
                                <el-breadcrumb-item @click.native="toIndex" :to="{path: '/index'}">首页</el-breadcrumb-item>
                                  <transition-group name="breadcrumb">
                                      <el-breadcrumb-item
                                      v-for="route in $route.matched"
                                      v-if="route.meta.title!='首页'&&route.meta.title"
                                      :key="route.path"
                                      >
                                    {{ route.meta.title }}
                                    </el-breadcrumb-item>
                                  </transition-group>
                                </el-breadcrumb>
                          </div>
                      </div>
                      <div class="right-menu">
                        <div class="iList">
                          <div class="search">
                            <i @click.stop="expandInput" class="iconfont icon-sousuo"></i>
                            <el-autocomplete
                              :style="{width: inputExpanded ? '200px' : '0'}"
                              class="inline-input"
                              @blur="searchBlur"
                              v-model="searchText"
                              ref="autofocus"
                              :fetch-suggestions="querySearch"
                              placeholder="Search"
                              :trigger-on-focus="false"
                              @select="handleSelect">
                            <template v-slot:default="{item}">
                              {{item.title}}
                            </template>
                            </el-autocomplete>
                          </div>
                          <el-tooltip class="tooltip" effect="dark" content="全屏缩放" placement="bottom">
                              <i @click="toggleFullscreen" :class="fullScreen?'iconfont icon-suoxiao':'iconfont icon-quanping'"></i>
                          </el-tooltip>
                          <el-tooltip class="tooltip" effect="dark" content="布局设置" placement="bottom">
                              <i class="iconfont icon-zitidaxiao"></i>
                          </el-tooltip>
                        </div>
                        <el-dropdown class="avatar-container" trigger="click">
                          <div class="avatar-wrapper">
                            <img src="../../assets/avatar.jpg" class="user-avatar"/>
                            <i class="el-icon-caret-bottom" />
                          </div>
                          <el-dropdown-menu slot="dropdown" class="user-dropdown">
                            <router-link to="/">
                              <el-dropdown-item>
                                个人中心
                              </el-dropdown-item>
                            </router-link>
                            <el-dropdown-item divided @click.native="loginOut">
                              <span style="display:block;">退出登录</span>
                            </el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </div>
                  </div>
                  <div class="header_bottom">
                      <div class="tags">
                          <el-tag
                          :class="tag.active?'active':''"
                            v-for="tag in tags"
                            :disable-transitions="true"
                            :key="tag.path"
                            @contextmenu.native.prevent="CustomizeMenu(tag, $event)"
                            @close="tagClose(tag)"
                            @click="changeTag(tag.path,true)"
                            :closable="tag.name!='首页'"
                            :type="tag.type">
                            {{tag.name}}
                          </el-tag>
                      </div>
                  </div>
                </el-header>
                <el-main :style="{background:bg}">
                  <div class="overflowhidden">
                    <transition
                      name="fade-transform"
                      mode="out-in">
                    <router-view
                    v-if="ifRouterAlive"
                    ></router-view>
                    </transition>
                </div>
                </el-main>
                <el-footer>
                  <span class="beian">© 2019-2021 YSHOP All Rights Reserved ⋅ 豫ICP备17049587号</span>
                </el-footer>
            </el-container>
        </el-container>
        <ul ref="tagMenu" class="contextmenu">
          <li @click="RefreshTag">刷新</li>
          <li @click="tagClose(currentTag)" v-if="this.currentTag.path!='/index'">关闭</li>
          <li @click="CloseOther(currentTag)">关闭其他</li>
          <li @click="colseAlltag">关闭全部</li>
        </ul>
    </div>
</template>
<script>
import {SetCookie,GetCookie,RemoveCookie,setStore,getStore} from '@/config/cookie'
export default {
    name:'home',
   
    data() {
      return {
          seaechResult:[],
          ifRouterAlive: true,
          bg:'',
          inputExpanded:false,//是否显示输入框
          searchText:'',
          leftNavList:[],
          currentTag:{},//当前右击tag
          fullScreen:false,//全屏
          isCollapse:false,//菜单收起状态
          tags:[
            {name:'首页',path:'/index',active:true}
          ]
      }
    },
    watch: {
      $route(to,from){
        if(to.path=="/index"){this.bg = '#F0F2F5'}else{this.bg = ''}
        if(to.path!='/login'&&to.path!='*'){
          this.$NProgress.start()
          this.$NProgress.done()
        }
        let canAdd = true;
        for(let item of this.tags){
          item.active = false
          if(item.path==this.$router.currentRoute.path){
              canAdd = false
          }
        }
        if(canAdd&&this.$router.currentRoute.name!='index'){
          this.tags.push({
              name:this.$router.currentRoute.meta.title,
              path:this.$router.currentRoute.path,
              active:true
          })
        }
      },
    },
    created() {
      if(this.$route.path=="/index"){this.bg = '#F0F2F5'}
      window.addEventListener('beforeunload',(e)=>{
        this.tags.splice(1);
        if(this.$router.currentRoute.meta.title!='首页'){
          this.tags.push({
            name:this.$router.currentRoute.meta.title,
              path:this.$router.currentRoute.path,
              active:true
          })
        }
        SetCookie('tags',JSON.stringify(this.tags));
      })
    },
    mounted() {
      if(getStore("NavList")){
        this.leftNavList = JSON.parse(getStore("NavList"))
      }else{
        this.getRoutes()
        }
      document.addEventListener('click',()=>{
        this.$refs.tagMenu.style.display = 'none';
      })
      if(GetCookie('tags')){
          this.tags=JSON.parse(GetCookie('tags'))
          if(this.tags.length>1){
            this.tags[0].active=false
          }
      }
      // 全屏状态
      document.addEventListener('fullscreenchange', () => {
        if(!!document.fullscreenElement){
          this.fullScreen = true
        }else{
          this.fullScreen = false
        }
      })
    },
    beforeDestroy() {
      window.removeEventListener('beforeunload',()=>{});
      RemoveCookie('tags')
      document.removeEventListener('fullscreenchange',()=>{});
    }, 
    methods: {
      // 路由占位
      reload() {
      this.ifRouterAlive = false;
      this.$nextTick(() => {
        this.ifRouterAlive = true;
      });
    },
      //搜索失焦
       searchBlur(){
        this.searchText='';
        this.valueKey = 'value' + Math.random().toString(36).substring(2);
        this.inputExpanded=false;
    },
      // 搜索弹出过滤
      querySearch(queryString, cb) {
        const results = [];
            for(let item of this.leftNavList){
              if(item.meta.title.indexOf(queryString)!=-1){
                 if(item.children){
                  for(let itemchild of item.children){
                    results.push({
                      title:`${item.meta.title}>${itemchild.meta.title}`,
                      path:itemchild.path
                    });
                  }
                 }
              }else{
                for(let itemchild of item.children){
                  if(itemchild.meta.title.indexOf(queryString)!=-1){
                    results.push({
                      title:`${item.meta.title}>${itemchild.meta.title}`,
                      path:itemchild.path
                    });
                  }
                }
              }
            }
        cb(results);
      },
      // 选中搜索项
      handleSelect(item) {
        console.log(item);
        this.changeTag(item.path,1)
        this.inputExpanded=false;
        this.searchText=''
      },
      // 点击搜索图标
      expandInput() {
      this.inputExpanded = !this.inputExpanded;
        if (!this.inputExpanded) {
          this.searchText = ''; // 清空搜索文本
        }
        if(this.inputExpanded){
          this.$nextTick(()=>{
            this.$refs.autofocus.focus()
            this.$refs.autofocus.activated = true
          })
        }
      },
      // tag右击菜单
      CustomizeMenu(tag,e){
        this.currentTag = tag;
        this.$refs.tagMenu.style.display = 'block';
        this.$refs.tagMenu.style.left = `${e.x+10}px`;
        this.$refs.tagMenu.style.top = `${e.y}px`;
      },
      // 关闭其他tag
      CloseOther(currentTag){
        for(let i=this.tags.length-1;i>=0;i--){
            if(this.tags[i].path!='/index'&&this.tags[i].path!=currentTag.path){
            this.tags.splice(i,1);
          }
          this.changeTag(currentTag.path,1)
        }
      },
      // 当前tag刷新
      RefreshTag(){
        if(this.$route.path==this.currentTag.path)this.reload();
        this.$router.push({path:this.currentTag.path});
        this.changeTag(this.currentTag.path);
      },
      // 关闭全部tag
      colseAlltag(){
        this.tags.splice(1)
        this.changeTag('/index',1)
      },
      // 关闭tag
      tagClose(tag){
        this.tags.forEach((item,index)=>{
          if(item.path == tag.path){
            this.tags.splice(index,1)
            if(item.path==this.$router.currentRoute.path){
              this.$router.push(this.tags[this.tags.length-1].path);
              this.$nextTick(()=>{
                this.tags[this.tags.length-1].active=true
              })
            }
          }
        })
      },
      //导航切换tags || //tag切换路由
      changeTag(path,istag){
        console.log(path);
        if(istag){
          this.$router.push(path);
        }
        for(let item of this.tags){
          item.active=false
          this.$nextTick(()=>{
            if(item.path==path){
              item.active=true
            }
          })
        }
      },
      //退出登录
      loginOut() {
        this.$confirm('确定注销并退出系统吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          RemoveCookie('token')
          this.$router.push('/login')
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          });
        });
      },
      //切换全屏
      toggleFullscreen() {
      const doc = window.document;
      const docEl = doc.documentElement;
      
      const requestFullScreen = docEl.requestFullscreen || docEl.mozRequestFullScreen || docEl.webkitRequestFullScreen || docEl.msRequestFullscreen;
      const cancelFullScreen = doc.exitFullscreen || doc.mozCancelFullScreen || doc.webkitExitFullscreen || doc.msExitFullscreen;
      // 判断是否已经全屏
      this.fullScreen = !this.fullScreen
      if (!doc.fullscreenElement && !doc.mozFullScreenElement && !doc.webkitFullscreenElement && !doc.msFullscreenElement) {
        // 进入全屏
        requestFullScreen.call(docEl);
      } else {
        // 退出全屏
        cancelFullScreen.call(doc);
      }
      },
      // 点击面包屑首页
      toIndex(){
        if(this.$route.path=="/index"){
          this.tags[0].active=true
        }
      },
      // 获取路由信息
      getRoutes(){
        this.leftNavList = this.$router.options.routes.splice(3);
        if(this.leftNavList.length>0)setStore('NavList',JSON.stringify(this.leftNavList))
      },
      // 收起导航
      CollapseMenu(){
        this.isCollapse = !this.isCollapse;
      }
    }
}
</script>
<style>
  .search{
    display: flex;
    align-items: center;
  }
  .inline-input {
    width: 0;
    transition: all 0.3s;
  }
  .inline-input.expanded {
    width: 200px;
  }
  .inline-input .el-input__inner{
    line-height: 32px;
    height: 32px;
    border-radius: 0;
    border: 0;
    padding-left: 0;
    padding-right: 0;
    box-shadow: none !important;
    border-bottom: 1px solid #409EFF;
    vertical-align: middle;
  }
  .contextmenu{
    display: none;
    margin: 0;
    background: #fff;
    z-index: 3000;
    position: absolute;
    z-index: 9999;
    top: 0;
    left: 0;
    list-style-type: none;
    padding: 2px 0;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 400;
    color: #333;
    box-shadow: 2px 2px 3px 0 rgba(0,0,0,.3);
  }
  .contextmenu>li{
    padding: 7px 16px;
    margin: 0;
    cursor: pointer;
    box-sizing: border-box;
  }
  .contextmenu>li:hover{
    background-color: #eee;
  }
  @media screen and (max-width: 1000px) {
    .el-menu-vertical-demo {
      display: none;
    }
    .iList{
      display: none !important;
    }
  }
  @media screen and (max-width: 340px) {
    .right-menu{
      display: none !important;
    }
  }
  @media screen and (max-width: 270px) {
    .el-breadcrumb{
      display: none;
    }
  }
  .el-breadcrumb{
    min-width: 194px;
    overflow: hidden;
  }
  .el-tag__close:hover{
    background-color: #B4BCCC!important;
  }
  .tags .el-tag i{
    color: #495060;
    font-size: 10px;
  }
  .tags .active i{
    color: #fff;
    font-size: 10px;
  }
  .tags .el-tag{
    cursor: pointer;
    height: 26px;
    line-height: 25px;
    font-size: 12px;
    border: 1px solid #D8DCE5;
    border-radius:0;
    margin-right: 8px;
    color: #495060;
    background-color: #fff!important;
  }
  .tags .active::before{
    content: "";
    background: #fff;
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    position: relative;
    margin-right: 2px;
  }
  .tags .active{
    background-color: #42B983!important;
    color: #fff;
  }
  .tags{
    white-space: nowrap;
    overflow-x: scroll;
    width: 100%;
    max-height: 28px;
    scrollbar-width: none; /* firefox */
    -ms-overflow-style: none; /* IE 10+ */
  }
  .tags::-webkit-scrollbar {
    display: none; /* Chrome Safari */
  }
  .header_bottom{
    padding: 3px 0;
    max-height: 30px;
    box-sizing: border-box;
    width: 100%;
  }
  .right-menu {
      display: flex;
      align-items: center;
  }
  .avatar-container {
    margin-right: 30px;
  }
  .avatar-wrapper {
    margin-top: 5px;
    position: relative;
  }
  .user-avatar {
    cursor: pointer;
    width: 40px;
    height: 40px;
    border-radius: 6px;
  }
  .el-submenu__title *{
    vertical-align: top!important;
  }
  .el-menu-vertical-demo span{
    float: left;
  }
  .el-icon-caret-bottom {
    cursor: pointer;
    position: absolute;
    right: -20px;
    top: 25px;
    font-size: 12px;
  }

  .navTopLogo:hover{
    background-color: #304156!important;
  }
  .header_top{
    box-sizing: border-box;
    border-bottom: 1px solid #F3F4F4;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .Breadcrumb{
    margin-left: 10px;
  }
  .CollapseMenu_Breadcrumb{
    display: flex;
    align-items: center;
    line-height: 50px;
  }
  .el-menu-item i{
    margin-left: 26px;
  }
    .indexMargin i{
    margin-left: 0;
  }
  .CollapseMenu_Breadcrumb>i{
      font-size: 22px;
      color: #000!important;
      padding: 0 5px;
      cursor: pointer;
  }
  .leftNavLogo{
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .leftNavLogo>div{
    margin-left: 5px;
    font-weight: bold;
    color: #fff;
    font-size: 14px;
    height: 100%;
  }
  .el-menu-vertical-demo li:first-child{
    padding: 0!important;
  }
  .el-menu-vertical-demo:not(.el-menu--collapse) {
      width: 200px;
      min-height: 400px;
    }
    .main{
        height: 100vh;
    }
  .el-aside {
    background-color: #D3DCE6;
    text-align: center;
    line-height: 200px;
  }
  body > .el-container {
    margin-bottom: 40px;
  }
  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }
  
  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }
  .el-menu-item-group__title{
    padding: 0;
  }
  .el-submenu .el-menu-item{
    padding: 0;
  }
  .navItem{
    background-color: #1F2D3D!important;
  }
  .navItem:hover{
    background-color: #001528!important;
  }
  .el-aside{
    overflow-y: scroll;
    background-color: #304156;
    scrollbar-width: none; /* firefox */
    -ms-overflow-style: none; /* IE 10+ */
  }
  .el-aside::-webkit-scrollbar {
    display: none; /* Chrome Safari */
  }
  .el-header, .el-footer{
    text-align: left;
    background-color: #fff;
  }
  .el-header{
    border-bottom: 2px #D8DCE5 solid;
    height: 84px!important;
    padding: 0 14px;
    line-height: 14px;
    background-color: #fff;
  }
  .el-footer{
    z-index: 1003;
    position: fixed;
    bottom: 0;
    box-sizing: border-box;
    border-top: #E7EAEC solid 1px;
    width:100%;
    height: 34px!important;
    line-height: 33px;
    display: flex;
    justify-content: flex-start;
  }
  .iList{
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
  .iList i{
    font-size: 20px;
    color: #5A5E66!important;
    margin: 0 10px;
    cursor: pointer;
  }
  .iconfont{
    font-size: 16px;
    float: left;
    margin-right: 20px;
    color: #BFCBD9!important;
  }
  .router-link-active{
    text-decoration: none;
  }
    /* 二级动画 */
    /* fade-transform */
    .overflowhidden{
      overflow: hidden;
    }
    .fade-transform-leave-active,
    .fade-transform-enter-active {
      transition: all .3s;
    }

    .fade-transform-enter {
      opacity: 0;
      transform: translateX(-30px);
    }

    .fade-transform-leave-to {
      opacity: 0;
      transform: translateX(30px);
    }
    /* breadcrumb transition */
    .breadcrumb-enter-active,
    .breadcrumb-leave-active {
      transition: all .5s;
    }

    .breadcrumb-enter,
    .breadcrumb-leave-active {
      opacity: 0;
      transform: translateX(20px);
    }
    .breadcrumb-move {
      transition: all .5s;
    }
    .breadcrumb-leave-active {
      position: absolute;
    }
  .beian{
    font-size: 12px;
    color: #7A8B9A;
    max-height: 33px;
    overflow: hidden;
  }
</style>